package com.zdd.ehe.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;


import java.util.Collections;
import java.util.List;

/**
 * @version 1.0.0
 * @title: Swagger3Config
 * @author: HFLy
 * @description: swagger3配置
 * @date: 9/11/2022 下午8:51
 *
 * 2023/03/12 修改
 * 添加jwt验证 暂未使用
 *
 */
@Configuration
public class Swagger3Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                /*.securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()))*/;
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^!auth.*$"))
                .build();
    }

    private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections.singletonList(new SecurityReference("token", authorizationScopes));
    }

    private SecurityScheme securityScheme() {
        return new ApiKey("token","token","header");
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("计设--接口文档")
                .description("更多请咨询服务开发者Hfly")
                .contact(new Contact("Hfly", "https://github.com/clxzl?tab=repositories", "hf152175@foxmail.com"))
                .version("1.0")
                .build();
    }
}
